package com.yeswater.multithread.chapter1.test1_11.test3;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * author : yeswater
 * date : 2023/10/18
 */

public class RunTest {

    @Test
    public void test1() throws InterruptedException {
        MyThread t = new MyThread();
        t.start();
        boolean list1IsInterrupted = false;
        boolean list2IsInterrupted = false;
        while (t.isAlive()) {
            if (Box.list1.size() > 500 && !list1IsInterrupted) {
                t.interrupt();
                list1IsInterrupted = true;
            }
            if (Box.list2.size() > 600 && !list2IsInterrupted) {
                t.interrupt();
                list2IsInterrupted = true;
            }
            TimeUnit.MILLISECONDS.sleep(60);
        }
    }

    @Test
    public void test2() throws InterruptedException {
        MyThread1 t = new MyThread1();
        t.start();
        boolean list1IsInterrupted = false;
        boolean list2IsInterrupted = false;
        while (t.isAlive()) {
            if (Box.list1.size() > 500 && !list1IsInterrupted) {
                t.interrupt();
                list1IsInterrupted = true;
            }
            if (Box.list2.size() > 600 && !list2IsInterrupted) {
                t.interrupt();
                list2IsInterrupted = true;
            }
            TimeUnit.MILLISECONDS.sleep(60);
        }
    }

}
